package com.hjw.weixun.dao.impl;

import java.util.List;

import com.hjw.weixun.Template.JdbcTemplate;
import com.hjw.weixun.dao.UserDao;
import com.hjw.weixun.domain.User;
import com.hjw.weixun.domain.Weixun;

public class UserDaoJdbcImpl extends JdbcTemplate<User> implements UserDao {
	public static UserDao userDao = null;

	// 单例
	public static UserDao getInstance() {
		if (userDao == null) {
			userDao = new UserDaoJdbcImpl();
		}
		return userDao;
	}

	@Override
	public void addUser(User user) {
		String username = user.getUsername();
		String passeord = user.getPassword();
		String sql = "insert into user(username,password) values (?,?) ";
		Object[] args = new Object[] { username, passeord };
		this.execute(sql, args);
	}

	@Override
	public User getUser(User user) {
		String username = user.getUsername();
		String passeord = user.getPassword();
		Object[] args = new Object[] { username, passeord };
		String sql = "select * from user where username = ? and password = ?";

		user = uniqueResult(sql, args);
		return user;
	}

	@Override
	public List<User> getFollowedList(Weixun weixun) {
		String sql = "select * from user u join follower f on f.followerid = u.userid and f.weixunid = ? ";
		Object[] args = new Object[] { weixun.getId() };

		return this.executeQueryAllToList(sql, args);

	}

}
